from typing import Optional

import model
from model.uml_element import UmlElement
from model.uml_constraint import UmlConstraint
from model.uml_generalization import UmlGeneralization
from model.uml_property import UmlProperty
from model.uml_operation import UmlOperation
from model.uml_xmi_info import UmlXmiInfo



class UmlClass(UmlElement):
    def __init__(self, xmi_type=UmlXmiInfo.type_class) -> None:
        super().__init__(xmi_type)
        self.name: str = ""
        self.isAbstract: str = ""
        self.ownedRules: list[UmlConstraint] = []
        self.generalizations: list[UmlGeneralization] = []
        self.ownedAttributes: list[UmlProperty] = []
        self.ownedOperations: list[UmlOperation] = []
        # assistant fields
        self.package: Optional[model.UmlPackage] = None  
        self.isPartialElement: bool | None= None
        self.baseClass = ""

    def to_json(self) -> dict[str, object]:
        json: dict = super().to_json()
        json["name"] = self.name
        if self.isPartialElement:
            json["isPartialElement"] = self.isPartialElement
        if self.baseClass:
            json["baseClass"] = self.baseClass
        json["isAbstract"] = self.isAbstract

        if self.ownedComment is not None:
            json["ownedComment"] = self.ownedComment.to_json()

        item = []
        for value in self.ownedRules:
            item.append(value.to_json())
        json["ownedRules"] = item

        item = []
        for value in self.generalizations:
            item.append(value.to_json())
        json["generalizations"] = item

        item = []
        for value in self.ownedAttributes:
            item.append(value.to_json())
        json["ownedAttributes"] = item

        item = []
        for value in self.ownedOperations:
            item.append(value.to_json())
        json["ownedOperations"] = item

        return json
